package com.ruoyix.weblog.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.core.validate.QueryGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.weblog.domain.vo.BlogSortVo;
import com.ruoyix.weblog.domain.bo.BlogSortBo;
import com.ruoyix.weblog.service.IBlogSortService;
import com.ruoyix.common.core.page.TableDataInfo;

/**
 * 分类
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/weblog/sort")
public class BlogSortController extends BaseController {

    private final IBlogSortService iBlogSortService;

    /**
     * 查询分类列表
     */
    @SaCheckPermission("weblog:sort:list")
    @GetMapping("/list")
    @SaIgnore
    public TableDataInfo<BlogSortVo> list(BlogSortBo bo, PageQuery pageQuery) {
        return iBlogSortService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出分类列表
     */
    @SaCheckPermission("weblog:sort:export")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BlogSortBo bo, HttpServletResponse response) {
        List<BlogSortVo> list = iBlogSortService.queryList(bo);
        ExcelUtil.exportExcel(list, "分类", BlogSortVo.class, response);
    }

    /**
     * 获取分类详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("weblog:sort:query")
    @GetMapping("/{id}")
    @SaIgnore
    public R<BlogSortVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBlogSortService.queryById(id));
    }

    /**
     * 新增分类
     */
    @SaCheckPermission("weblog:sort:add")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogSortBo bo) {
        return toAjax(iBlogSortService.insertByBo(bo));
    }

    /**
     * 修改分类
     */
    @SaCheckPermission("weblog:sort:edit")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogSortBo bo) {
        return toAjax(iBlogSortService.updateByBo(bo));
    }

    /**
     * 删除分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("weblog:sort:remove")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBlogSortService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
    @SaIgnore
    @PostMapping("/selectBlogListWithLabel")
    public R<List<BlogSortVo>> selectBlogListWithLabel(){
        List<BlogSortVo> blogSortVos = iBlogSortService.selectBlogListWithLabel();
        return R.ok(blogSortVos);
    }
}
